#!/usr/local/bin/python3
# -*- coding: utf-8 -*-

"""
@File    : func.py
@Author  : Link
@Time    : 2022/5/1 23:41
@Mark    : 
"""

import math
import time
import datetime
import random


class SystemOS:
    Windows = "windows"
    Macos = "macos"
    Linux = "linux"
    Other = ""


def get_platform():
    import platform
    sys_platform = platform.platform().lower()
    if "windows" in sys_platform:
        return SystemOS.Windows
    elif "macos" in sys_platform:
        return SystemOS.Macos
    elif "linux" in sys_platform:
        return SystemOS.Linux
    else:
        return SystemOS.Other


def tid_maker():
    """
    生成唯一ID
    :return:
    """
    key_str = '{0:%Y%m%d%H%M%S%f}'.format(datetime.datetime.now()) + ''.join(
        [str(random.randint(0, 9)) for i in range(5)])
    return key_str


def datetime_to_mktime(date_time: datetime) -> int:
    return int(time.mktime(date_time.timetuple()))


def datetime_str_to_mktime(date_time: str) -> int:
    if date_time is None:
        return 0
    if date_time == "NULL":
        return 0
    time_array = time.strptime(date_time, "%Y-%m-%d %H:%M:%S")
    return int(time.mktime(time_array))


def timestamp_to_str(timestamp: int) -> str:
    if timestamp is None:
        return "NULL"
    date_array = datetime.datetime.utcfromtimestamp(timestamp)
    return date_array.strftime("%Y-%m-%d %H:%M:%S")


def timestamp_to_str2(timestamp: int) -> str:
    if timestamp is None:
        return "NULL"
    date_array = datetime.datetime.utcfromtimestamp(timestamp)
    return date_array.strftime("%Y%m%d%H%M%S")


# 获取当前时间戳
def get_now_timestamp():
    return int(int(datetime.datetime.now().timestamp()))


# 计算 UI应该显示在哪个位置
def ui_location(socket_count: int, dut: int, width: int, height: int, offset_x: int, offset_y: int):
    """

    :param socket_count:
    :param dut:
    :param width:
    :param height:
    :param offset_x:
    :param offset_y:
    :return:
    """
    row = math.ceil(math.sqrt(socket_count))
    x, y = divmod(dut, row)
    move_x = x * width + offset_x
    move_y = y * height + offset_y
    return move_x, move_y

